package com.lzy.mall.coupon.service;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.lzy.mall.coupon.entity.SmsSeckillSession;
import com.lzy.mall.coupon.dao.SmsSeckillSessionDao;
import com.lzy.mall.coupon.entity.SmsSeckillSkuRelation;
import com.lzy.mall.coupon.vo.SeckillSessionWithSkusVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 秒杀活动场次(SmsSeckillSession)表服务
 *
 * @author lzy
 * create on 2021-10-14 11:06:27
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class SmsSeckillSessionService {
    private final SmsSeckillSessionDao smsSeckillSessionDao;

    private final SmsSeckillSkuRelationService seckillSkuRelationService;

    public SmsSeckillSession queryById(Long id) {
        return smsSeckillSessionDao.single(id);
    }

    public List<SmsSeckillSession> queryAll() {
        return smsSeckillSessionDao.all();
    }

    @Transactional(rollbackFor = Exception.class)
    public void insert(SmsSeckillSession smsSeckillSession) {
        smsSeckillSessionDao.insertTemplate(smsSeckillSession, true);
    }

    /**
     * 根据主键更新记录
     *
     * @param smsSeckillSession 待更新的记录.包含主键在内，如果其他列的值为空则不更新
     * @return 成功返回true，失败返回false
     */
    public boolean updateById(SmsSeckillSession smsSeckillSession) {
        return smsSeckillSessionDao.updateTemplateById(smsSeckillSession) > 0;
    }

    /**
     * 根据主键删除记录
     *
     * @param id 主键
     * @return 成功返回true，反之返回false
     */
    public void deleteById(List<Long> id) {
        smsSeckillSessionDao.createLambdaQuery()
                .andIn(SmsSeckillSession::getId, id)
                .delete();
    }

    /**
     * 分页查询
     *
     * @param pageQuery 封装分页条件的分页对象
     * @return pageQuery 作为一个引用，将查询结果封装进去
     */
    public PageQuery<SmsSeckillSession> queryByPage(PageQuery<SmsSeckillSession> pageQuery) {
        smsSeckillSessionDao.queryByPage(pageQuery);
        return pageQuery;
    }

    public List<SeckillSessionWithSkusVo> uploadSecKillSkuLatest() {
        DateTime now = DateUtil.date();
        DateTime end = DateUtil.offsetDay(now, 1);
        DateTime startTime = DateUtil.beginOfDay(now);
        DateTime endTime = DateUtil.endOfDay(end);
        List<SmsSeckillSession> seckillSessions = smsSeckillSessionDao.createLambdaQuery()
                .andBetween(SmsSeckillSession::getStartTime, startTime, endTime)
                .select();
        List<SmsSeckillSkuRelation> seckillSkuRelations = seckillSkuRelationService.queryAll();
        List<SeckillSessionWithSkusVo> sessionWithSkusTos = seckillSessions.stream().map(session -> {
            SeckillSessionWithSkusVo to = new SeckillSessionWithSkusVo();
            BeanUtil.copyProperties(session,to);

            //秒杀活动商品关联
            List<SmsSeckillSkuRelation> relations = seckillSkuRelations.stream()
                    .filter(sku -> sku.getPromotionSessionId().equals(session.getId()))
                    .collect(Collectors.toList());
            to.setRelationEntities(relations);
            return to;
        }).collect(Collectors.toList());
        return sessionWithSkusTos;
    }
}
